
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述: 第三方设置登录密码请求类
 *
 * @author jamywang
 * @since 2015年12月8日 下午2:57:37
 */
public class SetLoginPsdTask {

    public SetLoginPsdResponse request(String userName, String newPwd) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        SetLoginPsdResponse respObj = new SetLoginPsdResponse();
        ResponsePackage resp = new LoginPsdResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_SET_LOGIN_PSD);
        params.put("username", userName);
        params.put("newpassword", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(newPwd, "utf-8")));
        requestParams.add(params);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    static class LoginPsdResponsePackage extends HttpBaseResponsePackage<SetLoginPsdResponse> {

        @Override
        protected void handleResponse(SetLoginPsdResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "SetLoginPsdTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (response.isUseful() && ProtocolCmd.CMD_SET_LOGIN_PSD.equals(cmd)) {
                        JSONObject data = jsonResp.optJSONObject("data");
                        if (!data.isNull("unreadmails")) {
                            response.setUnreadmails(data.optInt("unreadmails"));
                            response.setOk(true);
                        }
                    }
                }
            } catch (Exception e) {
            }
        }

    }

    public static class SetLoginPsdResponse extends ResponseMessage {

        private int unreadmails;

        public int getUnreadmails() {
            return unreadmails;
        }

        public void setUnreadmails(int unreadmails) {
            this.unreadmails = unreadmails;
        }

    }
}
